import axios,{AxiosRequestConfig} from "axios"
import {addReq,removeReq} from "./restrictReq"
import Cookies from 'js-cookie';
import { message } from 'ant-design-vue';

const Request = axios.create({
    baseURL:'http://sit.pm-api.yolanda.hk/',
    timeout:60000,
});

let reqList:string[] = []


Request.interceptors.request.use((config:AxiosRequestConfig) => {   //对数据请求进行拦截
    addReq(config)
    // 阻止重复请求。当上个请求未完成时，相同的请求不会进行
    if (config?.url?.match(/login/)) {   //当打印pdf文件时
        return config
    }
    if (Cookies.get("userInfo")) {   //登录后添加验证签名
        config.headers['token'] = JSON.parse(Cookies.get("userInfo") || '').token;
    }
    return config
}, error => {
    return Promise.reject(error)
})

Request.interceptors.response.use(  //对数据回复进行拦截	
    response => {
        if(response.data.code==201){
            message.error(response.data.msg)
        }
        removeReq(response.config)
        return response
    },
    error => {
        //  	alert("当前服务异常，请稍候再试！")
        if (axios.isCancel(error)){  //判断异常是否由取消引起
            console.log(error);
        } else {
            // 增加延迟，相同请求不得在短时间内重复发送
            //allowRequest(reqList, error.config.url)
            removeReq(error)
        }
        return Promise.reject(error.data)
    }
)

export default Request;